【配置文件】读取
configparser
功能:读取、写出配置文件
支持:.ini,.conf 文件
常用函数
- 构建
import ConfigParser cf = ConfigParser.ConfigParser() cf.read(path)
- 获取配置文件列表
print cf.sections()
- 获取配置文件内容
print cf.get(section, option)
- 将指定列表下的所有数据转化成字典
print dict(cf.items(section))
- 添加新的内容
cf.add_section(new_section) # 添加新的section cf.set(section, new_option, value) cf.write(open(path), 'w')
- 删除内容
cf.remove_section(section) cf.remove_option(section, option) cf.write(open(path), 'w')